package com.example.smartmedicalsystem.repository;

import com.example.smartmedicalsystem.entity.Medicine;
import com.example.smartmedicalsystem.entity.User;
import com.example.smartmedicalsystem.entity.UserMedicine;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;

public interface UserMedicineRepository extends JpaRepository<UserMedicine, Long> {
    List<UserMedicine> findByUser(User user);

    Optional<UserMedicine> findByUserAndMedicine(User user, Medicine medicine);
    // 根据用户ID和药品ID查询
    Optional<UserMedicine> findByUser_IdAndMedicine_Id(Long userId, Long medicineId);

    // 根据用户ID查询所有记录
    List<UserMedicine> findByUser_Id(Long userId);
    @Query("SELECT um FROM UserMedicine um JOIN FETCH um.medicine WHERE um.user = :user")
    List<UserMedicine> findWithMedicinesByUser(@Param("user") User user);
}